﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Core.Function.String
{

    /// <summary>
    /// greeting = Concat("Hello, ", "world!"); # Hello, world!
    /// </summary>
    public class InsertNode : FuncNode
    {
        public InsertNode()
            : base()
        {
            base.Value = "insert";
        }

        public override object Evaluate()
        {
            if (Parameters.Count < 0)
            {
                throw new ArgumentException(
                    "usage: insert(expr, position, expr2)");
            }

            int length = Parameters.Count;
            string expr = Convert.ToString(
                Parameters[length - 1].Evaluate());
            int pos = Convert.ToInt32(
                Parameters[length - 2].Evaluate());
            string expr2 = Convert.ToString(
                Parameters[length - 3].Evaluate());

            return expr.Insert(pos, expr2);
        }
    }
}
